package com.example.service;

import com.example.dao.UsersRepository;
import com.example.pojo.Users;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersRepository usersRepository;

    @Override
    @Cacheable(value = "users")
    public List<Users> findUserAll() {
        return this.usersRepository.findAll();
    }

    @Override
    @Cacheable(value = "users")
    public Users findUserById(Integer id) {
        return this.usersRepository.getOne(id);
    }

    @Override
    @Cacheable(value = "users", key = "#pageable.pageSize")
    public Page<Users> findUserByPage(Pageable pageable) {
        return this.usersRepository.findAll(pageable);
    }

    @Override
    @CacheEvict(value = "users", allEntries = true)
    public void saveUser(Users users) {
        this.usersRepository.save(users);
    }

}
